package com.oa.official.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.official.entity.wechat.WechatMenu;
import com.oa.official.mapper.WechatMenuMapper;
import com.oa.official.service.WechatMenuService;
import com.oa.official.vo.wechat.WechatMenuVo;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WechatMenuServiceImpl extends ServiceImpl<WechatMenuMapper, WechatMenu> implements WechatMenuService {
    @Resource
    private WxMpService wxMpService;

    @Override
    public List<WechatMenuVo> findMenuInfo() {
        List<WechatMenuVo> list = new ArrayList<>();

        //1 查询所有菜单list集合
        List<WechatMenu> menuList = baseMapper.selectList(null);

        //2 查询所有一级菜单 parent_id=0，返回一级菜单list集合
        List<WechatMenu> oneMenuList = menuList.stream()
                .filter(menu -> menu.getParentId() == 0)
                .collect(Collectors.toList());

        //3 遍历一级菜单list集合遍历，得到每个一级菜单
        for(WechatMenu wechatMenu : oneMenuList) {
            //一级菜单Menu ---> MenuVo
            WechatMenuVo oneWechatMenuVo = new WechatMenuVo();
            BeanUtils.copyProperties(wechatMenu,oneWechatMenuVo);

            //4 获取每个一级菜单里面所有二级菜单 id 和 parent_id比较
            //一级菜单id  和  其他菜单parent_id
            List<WechatMenu> twoMenuList = menuList.stream()
                    .filter(menu -> menu.getParentId().longValue() == wechatMenu.getId())
                    .collect(Collectors.toList());

            //5 把一级菜单里面所有二级菜单获取到，封装一级菜单children集合里面
            //List<Menu> -- List<MenuVo>
            List<WechatMenuVo> children = new ArrayList<>();
            for(WechatMenu twoMenu : twoMenuList) {
                WechatMenuVo twoMenuVo = new WechatMenuVo();
                BeanUtils.copyProperties(twoMenu,twoMenuVo);
                children.add(twoMenuVo);
            }
            oneWechatMenuVo.setChildren(children);
            //把每个封装好的一级菜单放到最终list集合
            list.add(oneWechatMenuVo);
        }
        return list;
    }


    /**
     * 推送本地自己的菜单，按照指定的格式推送给企业微信
     */
    @Override
    public void syncMenu() {
        //1 菜单数据查询出来，封装微信要求菜单格式
        List<WechatMenuVo> menuVoList = this.findMenuInfo();
        //菜单
        JSONArray buttonList = new JSONArray();
        for(WechatMenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            if(CollectionUtils.isEmpty(oneMenuVo.getChildren())) {
                one.put("type", oneMenuVo.getType());
                one.put("url", "http://weblanqiang.viphk.nnhk.cc/#"+oneMenuVo.getUrl());
            } else {
                JSONArray subButton = getObjects(oneMenuVo);
                one.put("sub_button", subButton);
            }
            buttonList.add(one);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);

        //2 调用工具里面的方法实现菜单推送
        try {
            wxMpService.getMenuService().menuCreate(button.toString());
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    @NotNull
    private static JSONArray getObjects(WechatMenuVo oneMenuVo) {
        JSONArray subButton = new JSONArray();
        for(WechatMenuVo twoMenuVo : oneMenuVo.getChildren()) {
            JSONObject view = new JSONObject();
            view.put("type", twoMenuVo.getType());
            if(twoMenuVo.getType().equals("view")) {
                view.put("name", twoMenuVo.getName());
                //H5页面地址
                view.put("url", "http://weblanqiang.viphk.nnhk.cc#"+twoMenuVo.getUrl());
            } else {
                view.put("name", twoMenuVo.getName());
                view.put("key", twoMenuVo.getMeunKey());
            }
            subButton.add(view);
        }
        return subButton;
    }

    @Override
    public void removeMenu() throws WxErrorException {
        wxMpService.getMenuService().menuDelete();
    }
}
